package com.invengo.testrfid.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.invengo.testrfid.R;
import com.invengo.testrfid.app.utils.MyUIUtils;
import com.invengo.testrfid.app.utils.SoundPlayUtils;
import com.invengo.testrfid.di.component.DaggerSearchComponent;
import com.invengo.testrfid.di.module.SearchModule;
import com.invengo.testrfid.mvp.contract.SearchContract;
import com.invengo.testrfid.mvp.presenter.SearchCagePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SearchActivity extends BaseActivity<SearchCagePresenter> implements SearchContract.View {

    @BindView(R.id.tv_tid)
    TextView tv_tid;

    @BindView(R.id.et_c_no)
    EditText et_c_no;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSearchComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .searchModule(new SearchModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_search; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        mPresenter.connectReader();
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    public void readTag(View view) {

        mPresenter.readTagTid();
    }

    @Override
    public void initTid(String tid) {

        SoundPlayUtils.getInstance().playSound(SoundPlayUtils.SOUNT_SUCCESS, 0);
        tv_tid.setText(tid);
    }

    @Override
    public void initCageNo(String x) {
        et_c_no.setText(x);
    }

    public void queryTag(View view) {
        mPresenter.queryBindByTid(tv_tid.getText().toString());
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyUp(keyCode, event);
        } else {
            mPresenter.stopReadTag();
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyDown(keyCode, event);
        } else {

            mPresenter.readTagTid();
            return true;
        }
    }

    @Override
    public void showLoading() {

        MyUIUtils.showProgressDia(this);
    }

    @Override
    public void hideLoading() {

        MyUIUtils.hideProgressDia(this);
    }

}
